package com.ph.demo.current;

/**
 * 对象锁
 */
public class SynchronizeDemo1 implements Runnable{

    static SynchronizeDemo1 synchronizeDemo1 = new SynchronizeDemo1();

    Object o = new Object();


    static int i = 0;

    @Override
    public void run() {
        for(int j = 0;j<1000000;j++){
            synchronized (o){

                i++;
            }

        }
    }

    public static void main(String[] args) throws Exception{
//        Thread thread = new Thread(new SynchronizeDemo1());
//        Thread thread1 = new Thread(new SynchronizeDemo1());
        Thread thread = new Thread(synchronizeDemo1);
        Thread thread1 = new Thread(synchronizeDemo1);
        thread.start();
        thread1.start();
        thread1.join();
        thread.join();  //等待线程执行结束
        System.out.println(i);
    }
}
